// JavaScript Document
var istop = false;
var istopedit = false;
var scwidth = 0;
var cookie_pre = 'ews_';
var gcookie = [];
var user = {
	user_id : 0,
	username : ''
};
var isfocused = false;
var syncselect = 0;
$(document).ready(function(){
	scwidth =$(document.body).width();
	
	//基本初始化
	$('#winbackground').hide();
	$('#win').hide();
	$('#msg').hide();
	$('#sync').hide();
	
	//头部透明事件
	$('#top').animate({opacity:0.8}, 100);
	$('#top').hover(
		function(){
			istop = true;
			$(this).animate({opacity:1.0}, 100);
		},
		function(){
			istop = false;
			!istopedit && $(this).animate({opacity:0.8}, 100);
		}
	);
	$('#top-input').blur(function(){
		$(this).val('做点什么呢？ 状态更新 | 任务发布 | 写邮件 | 启动交流');
		istopedit = false;
		!istop && $('#top').animate({opacity:0.8}, 100);
	});
	$('#top-input').focus(function(){
		istopedit = true;
		$(this).animate({opacity:1.0}, 100);
		$(this).val('');
	});
	
	//默认值
	$.ajaxSetup({
		error: ajax_error,
		type: 'POST',
		timeout: 300000
	});
	
	user.user_id = $.cookie.get('user_id');
	user.username = $.cookie.get('username');
	if(user.user_id > 0){
		init();
	}else{
		win_show('login', '/user/login');
	}
	/*******************************************************************
	 *                   用户登陆
	 ******************************************************************/
	
	//测试部分
	//setTimeout(test, 100);
	//test();
	/*******************************************************************
	 *                   邮件
	 ******************************************************************/
	$('#win').delegate('.mail-box-close','click',function(){
		$('#win').hide();
	})
	
});

function init(){
	body_load('/index/index');
}

var work = {
	finish:function(work_id){
		$.ajax({
			url : '/work/finish',
			dataType : 'json',
			data : {work_id: work_id, output: 'json'},
			success: function(data){
				if(data.msg){
					message(data.msg, 'error');
				}else{
					message('操作成功！', 'info');
					body_load('/work/index');
				}
			}
		});
	}
};

var notice = {
	read:function(notice_id, type, relate_id){
		switch(type){
			case 1:
				body_load('/doing/view', {doingid:relate_id});
				break;
			case 2:
				body_load('/forum/view', {forumid:relate_id});
				break;
			case 3:
				body_load('/work/view', {work_id:relate_id});
				break;
			case 4:
				body_load('/work/view', {work_id:relate_id});
				break;
		}
		frame_load('/notice/view?notice_id='+notice_id);
	}
};

var sync = {
	u:function(o){
		var o = $('#sync').children().children();
		var cur = syncselect < 2 ? o.length : syncselect - 1;
		if(syncselect>0){
			o.eq(syncselect-1).removeClass('select');
		}
		o.eq(cur-1).addClass('select');
		syncselect = cur;
	},
	d:function(){
		var o = $('#sync').children().children();
		var cur = syncselect == o.length ? 1:syncselect+1;
		if(syncselect<=o.length){
			o.eq(syncselect-1).removeClass('select');
		}
		o.eq(cur-1).addClass('select');
		syncselect = cur;
	},
	show:function(o,callback){
		var top = o.offsetTop + o.offsetHeight;
		var left = o.offsetLeft;
		$('#sync').css({top:top+'px',left:left+'px',width:o.offsetWidth+'px'});
		$('#sync').show();
		$('#sync').bind('mousedown', callback);
		$('#sync').bind('mousemove', sync.ce);
		isfocused = true;
	},
	hide:function(callback){
		$('#sync').hide();
		$('#sync').unbind('mousedown', callback);
		$('#sync').unbind('mousemove', sync.ce);
		isfocused = false;
	},
	ce:function(){
		$('#sync').children().children().eq(syncselect-1).removeClass('select');
		syncselect=0;
	},
	call:function(o,url){
		var value=$(o).val();
		var start = value.lastIndexOf(',');
		start = start < 0 ? 0 : start;
		value = value.substr(start+1);
		$.ajax({
			url : url,
			data : {key:value,output:'json'},
			dataType : 'json',
			success:function(data){
				var html = '<ul>';
				var us = data.usernames;
				for(var i=0; i<us.length; i++){
					html += '<li>'+us[i]+'</li>';
				}
				html += '</ul>';
				$('#sync').html(html);
			}
		});
	},
	work_init:function(o){
		sync.call(o,'/sync/username');
		sync.show(o,sync.work_cb);
	},
	work_cb:function(e){
		var o = window.event.target;
		var username = '"'+o.innerHTML+'"';
		var txt = $('#work-input');
		var value = txt.val();
		var start = value.lastIndexOf(',');
		start = start < 0 ? 0 : start+1;
		
		value = value.substring(0,start);
		value += username;
		txt.val(value);
		sync.hide(sync.work_cb);
		stop_event();
	},
	work_sm:function(){
		var o = $('#sync').children().children().eq(syncselect-1);
		var username = '"'+o.html()+'"';
		var txt = $('#work-input');
		var value = txt.val();
		var start = value.lastIndexOf(',');
		start = start < 0 ? 0 : start+1;
		
		value = value.substring(0,start);
		value += username;
		txt.val(value);
		o.removeClass('select');
		sync.hide(sync.work_cb);
		syncselect = 0;
		stop_event();
	}
};

function stop_event(){
	var e = window.event;
	e.returnValue = false;
	if (e.preventDefault){
		e.preventDefault();
	}
	e.cancelBubble = true;
	if (e.stopPropagation){
		e.stopPropagation();
	}
	if(window.event.returnValue){
		window.event.returnValue = false;
	}
}

function comment_feed(o){
	var o = $(o).parent();
	var id = o.attr('feedid');
	var state = o.attr('state');
	var c = o.next();
	if(state == 1){
		c.slideUp(100);
		o.attr('state','0');
		return;
	}
	$.ajax({
		url : '/feed/comment',
		dataType: 'html',
		data: {feed_id: id},
		success:function(data){
			c.hide();
			c.html(data);
			c.slideDown(100);
			o.attr('state', '1');
		}
	});
}

function frame_load(url){
	var o = $('#ajaxtarget');
	o.attr('src', url);
}

function body_load(url, data){
	if(!data){
		var data = {};
	}
	load_html($('#mbody'), url, data);
}

function load_html(o, url, data){
	$(o).fadeOut(200, function(){
		$.ajax({
			context:this,
			url:url,
			data:data,
			dataType: 'html',
			success: function(data){
				var o = $(this);
				o.html(data);
				o.fadeIn(200);
			}
		});
	});
}

function reply_this(o,ot){
	var reply_id = $(o).attr('replyid');
	var reply_name = $(o).attr('replyname');
	$(ot).val('回复 '+ reply_name + ' : ');
	$(ot).attr('replyid', reply_id);
	$(ot).focus();
}

function reply(o){
	var o = $(o);
	var relate_id = o.attr('relateid');
	var type = o.attr('replytype');
	var replyid = o.attr('replyid');
	var mes = o.val();
	o.val('');
	o.blur();
	$.ajax({
		url : '/reply/create',
		context: o,
		data: {type:type, relate_id:relate_id, replyid:replyid, output:'json', message:mes},
		dataType: 'json',
		success:function(data){
			if(data.code){
				message('回复成功','info');
				var o = $(this).parent().next();
				o.prepend('<li><a href="javascript:void(0);">'+ user['username'] +'</a> ： '+mes+'</li>');
			}else{
				message('回复失败', 'error');
			}
		}
	});
}

function update_doing(data){
	$.ajax({
		url: '/user/updatedoing',
		dataType: 'json',
		data: {message:data, output:'json'},
		success: function(data){
			if(typeof data.code != 'undefined'){
				if(data.code == 0){
					message('更新成功', 'info');
				}else{
					var msg = data.msg ? data.msg : '更新失败';
					message(msg, 'error');
				}
			}
		}
	});
}

function user_login(email, password){
	$.ajax({
		url: '/user/login',
		success: function(data){
			if(typeof data.code != 'undefined'){
				if(data.code == 0){
					win_hide('login');
					user.user_id = $.cookie.get('user_id');
					user.username = $.cookie.get('username');
					body_load('/index/index');
					return;
				}else{
					var msg = data.msg ? data.msg : '登陆失败';
					message(msg, 'error');
				}
			}else{
				var msg = data.msg ? data.msg : '登陆失败';
				message(msg, 'error');
			}
		},
		data: {email:email, password:password, output:'json'},
		dataType: 'json'
	});
}

function message(msg, type){
	var o = $('#msg');
	var type = type ? type : 'info';
	var rem = type == 'info' ? 'error' : 'info';
	o.removeClass(rem);
	o.addClass(type);
	o.html(msg);
	o.fadeIn(1000, function(){
		setTimeout("$('#msg').fadeOut(1000);", 2000);
	});
}

/***************************
 *      窗口
 ***************************/
function win_show(id, url){
	var winid = 'win-'+id;
	var b = $('#winbackground');
	if(!document.getElementById(winid)){
		var html = '<div id="'+winid+'"></div>';
		$('#win').prepend(html);
	}
	$.ajax({
		url: url,
		dataType: 'html',
		context: $('#'+winid),
		success: function(data){
			$('#win').show();
			b.show();
			var o = $('#'+winid);
			
			o.html(data);
			o.addClass('win');
			o.show();
			//框体与背景位置设置
			var left = (scwidth - 400)/2;
			o.css({left:left+'px'});
			b.css({left:(left - 10)+'px'});
			b.height(o.height()+20);
		},
		type: 'POST'
	});
}
function win_hide(id){
	var winid = 'win-'+id;
	var b = $('#winbackground');
	try{
		$('#win').hide();
		$('#'+winid).hide();
		b.hide();
	}catch(e){
	}
}
function ajax_error(x, t, e){
	alert(t);
}
function test(){
	$('#sync').show();
}


